import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

public class Practice02_DBCP {
    public static void main(String[] args) {
        Properties properties = new Properties();
        try {
            properties.load(new FileInputStream("JDBC/JDBC9.28/io/dbco.properties"));
            BasicDataSource dataSource = BasicDataSourceFactory.createDataSource(properties);
            Connection connection = dataSource.getConnection();
            System.out.println(connection);
            //SQL查询
            PreparedStatement statement = connection.prepareStatement("select * from account");
            ResultSet resultSet = statement.executeQuery();
            while (resultSet.next()){
                int id = resultSet.getInt("id");
                double balance = resultSet.getDouble("balance");
                String commit  = resultSet.getString("commit");
                System.out.println("编号：" + id + ",余额：" + balance + ",备注：" + commit);
            }
            resultSet.close();
            statement.close();
            connection.close();
        } catch (IOException | SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
